/**
 * Created by lt on 2017/5/10.
 */
$(function () {
    $("#form-admin-role-add").validate({
        rules: {
            roleName: {
                required: true
            },
        },
        onkeyup: false,
        focusCleanup: true,
        success: "valid",
        submitHandler: function (form) {
            var menuIds=getIds();
            var data={menuIds:menuIds}
            $("#form-admin-role-add").ajaxSubmit({
                cache: true,
                type: 'POST',
                data: data,
                url: '/role/saveMenuRole.html',
                async: false,
                dataType: "json",//返回值类型
                success: function (data) {
                    var data=$.parseJSON(data);
                    if (data.code != 1) {
                        layer.msg('操作失败!', {icon: 5, time: 1000});
                        console.log(data.msg);
                        return;
                    }
                    layer.msg('操作成功!', {icon: 6, time: 1000}, function () {
                        window.parent.location.reload();
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    })
                }
            })
        }
    });

    //查询角色已存在的权限
    var zNodes = [];
    var setting = {
        check: {
            enable: true
        },
        data: {
            simpleData: {
                enable: true
            }
        },
    };

    $(document).ready(function () {
        var data = {roleId: $("#tid").val()};
        $.ajax({
            type: 'post',
            url: '/role/getMenuListForRole.html',
            data: data,
            async: false,
            dataType: "json",
            success: function (data) {
                console.log(data);
                if (data.code != 1) {
                    alert(data.msg);
                    return;
                }
                var ddd = [];
                for (i = 0; i < data.data.length; i++) {
                    var roleId = data.data[i].roleId;
                    var menuParentid = data.data[i].menuParentId;
                    var open = false;
                    var checked = true;
                    if (menuParentid == 0) {
                        open = true
                    }
                    if (roleId == undefined) {
                        checked = false
                    }
                    var d = {
                        id: data.data[i].tid,
                        pId: data.data[i].menuParentId,
                        name: data.data[i].menuName,
                        checked: checked,
                        open: open
                    };
                    ddd.push(d);
                }
                $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
                var nodes = treeObj.getNodesByParam("id", "ssss", null);
                treeObj.addNodes(nodes[0], ddd);
            }
        });
    })

    function getIds(){
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var nodes = zTree.getCheckedNodes(true);
        var ids = [];
        if(nodes){
            var length  = nodes.length;
            for(var i=0;i<length;++i){
                var node = nodes[i];
                ids.push(node.id);
            }
        }
        return ids.join(",");
    }
});